package com.frogs.ucenter.service.impl;

import java.util.Arrays;

import org.apache.commons.lang.ArrayUtils;
import org.jsoup.Connection.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.web.api.entity.ApiUrl;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.system.web.api.response.Response.ResultParamEnum;
import com.frogs.ucenter.service.AbsService;
import com.frogs.ucenter.service.UserMessageDmService;
@Service("userMessageDmService")
public class UserMessageDmServiceImpl extends AbsService implements UserMessageDmService {
	private static Logger log = LoggerFactory.getLogger(UserMessageDmServiceImpl.class);
	
	@Override
	public JSONObject getUserDmList(Integer userId, Integer applicableId,
			Integer status, Integer level, Integer pageNo, Integer pageSize) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_DM_LIST");
		String[] otherParams = {
					ApiUrlParamEnum.receiverIds.getParamName(), userId.toString(),
					ApiUrlParamEnum.applicablePlatformId.getParamName(), applicableId.toString(),
					ApiUrlParamEnum.status.getParamName(), status.toString(),
					ApiUrlParamEnum.level.getParamName(), level.toString(),
					ApiUrlParamEnum.pageNo.getParamName(), pageNo.toString(),
					ApiUrlParamEnum.pageSize.getParamName(), pageSize.toString(),
					ApiUrlParamEnum.userId.getParamName(), userId.toString(),
					
				};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
			return result;
		} catch (Exception e) {
			log.error("func[getDmList] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}

	@Override
	public JSONObject userDmRead(Integer userId, Integer applicableId,
			String ids, String ip, Integer level) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_DM_READ");
		String[] otherParams = {
					ApiUrlParamEnum.receiverIds.getParamName(), userId.toString(),
					ApiUrlParamEnum.applicablePlatformId.getParamName(), applicableId.toString(),
					ApiUrlParamEnum.ids.getParamName(), ids,
					ApiUrlParamEnum.ip.getParamName(), ip,
					ApiUrlParamEnum.userId.getParamName(), userId.toString(),
					ApiUrlParamEnum.level.getParamName(), level.toString(),
					
				};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
			return result;
		} catch (Exception e) {
			log.error("func[userDmRead] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}

	@Override
	public JSONObject userDmUpdate(Integer userId, Integer applicableId,
			String ids, String ip, Integer status) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_DM_UPDATE");
		String[] otherParams = {
					ApiUrlParamEnum.receiverIds.getParamName(), userId.toString(),
					ApiUrlParamEnum.applicablePlatformId.getParamName(), applicableId.toString(),
					ApiUrlParamEnum.ids.getParamName(), ids,
					ApiUrlParamEnum.ip.getParamName(), ip,
					ApiUrlParamEnum.status.getParamName(), status.toString(),
					ApiUrlParamEnum.userId.getParamName(), userId.toString(),
					
				};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
			return result;
		} catch (Exception e) {
			log.error("func[userDmUpdate] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}

	@Override
	public JSONObject userDmCount(Integer userId, Integer applicableId, Integer level, Integer status) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_DM_COUNT");
		String[] otherParams = {
					ApiUrlParamEnum.receiverIds.getParamName(), userId.toString(),
					ApiUrlParamEnum.applicablePlatformId.getParamName(), applicableId.toString(),
					ApiUrlParamEnum.userId.getParamName(), userId.toString(),
					ApiUrlParamEnum.level.getParamName(), level.toString(),
					ApiUrlParamEnum.status.getParamName(), status.toString(),
					
				};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
			return result;
		} catch (Exception e) {
			log.error("func[userDmCount] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}

}
